/*
 *  bbmath.cpp
 *  BioBreak
 *
 *  Created by terry franguiadakis on 2/19/09.
 *  Copyright 2009 __MyCompanyName__. All rights reserved.
 *
 */

#include "bbmath.h"
#include <stdlib.h>
#if (0)
#include <OpenGLES/ES1/gl.h>
#include <OpenGLES/ES1/glext.h>
#endif


//const CRangeF CRangeF::ZeroOne;
//const CRangeI CRangeI::ZeroOne;


float GRandInRange(float min, float max)
{
//	LCASSERT( min <= max );
	return (float)( ( ((double)rand() / (double) RAND_MAX) * (max-min) + min) );
}

int SRandInRange(int min, int max)
{
//	LCASSERT( min <= max );
    
	double scale = (double)rand() / (double)RAND_MAX;
	//we need a unit interval for each possible (equally likely) value that randinrange
	//might return. there are max-min + 1 such intervals, so that's what "range" needs to be
	double range = (double)(max - min + 1);
    
	//this MIN is needed because if scale is close enough to 1, we could return max + 1
	//this is a low probability event, but can happen
	return BBMIN((int)(scale * range) + min, max);
}



/*--------------------------------------------------------------------------*/

bool IsNear(float g0, float g1, float epsilon)
{
	if(fabs(g0 - g1) < epsilon)
	{
		return true;
	}

	return false;
}

bool IsNearZero(float g, float epsilon)
{
	return IsNear(g, 0, epsilon);
}

bool IsPowerof2(int value) 
{
    while(value>1) 
    {
        if(value&1) return false;
        value>>=1;
    }
    
    return true;
}

 
/*--------------------------------------------------------------------------*/

const Vector2f Vector2f::Zero(0.0f, 0.0f);
const Vector2f Vector2f::One(1.0f, 1.0);
const Vector2f Vector2f::XNormal(1.0f, 0.0f);
const Vector2f Vector2f::YNormal(0.0f, 1.0f);
const Vector2f Vector2f::XNormalNeg(-1.0f, 0.0f);
const Vector2f Vector2f::YNormalNeg(0.0f, -1.0f);

/*--------------------------------------------------------------------------*/

void Transform2D::PushMatrix() const
{
    const float toDegrees = 180.0f / M_PI; 
#if (0)   
    // convert from transform to 4x4 
    // push on to stack
    ::glPushMatrix();
    ::glTranslatef(m_pos.x, m_pos.y, m_depth);
    ::glRotatef(m_zrot*toDegrees, 0.0f, 0.0f, 1.0f); 
    ::glScalef(m_scale, m_scale, m_scale);
#endif
}

void Transform2D::PopMatrix() const
{
    // symetric
#if (0)
    ::glPopMatrix();
#endif
}

void Transform2D::PushMatrixTmap() const
{
    const float toDegrees = 180.0f / M_PI;  // BB (TEF) fix
#if (0)
    ::glMatrixMode(GL_TEXTURE);
    ::glPushMatrix();
    ::glTranslatef(m_pos.x, m_pos.y, m_depth);
    ::glRotatef(m_zrot*toDegrees, 0.0f, 0.0f, 1.0f); 
    ::glScalef(m_scale, m_scale, m_scale);    
#endif
}

void Transform2D::PopMatrixTmap() const 
{
#if (0)
    ::glMatrixMode(GL_TEXTURE);
    ::glPopMatrix();
#endif
}

// NOTE these are static accessors
void Transform2D::glPushMatrix()
{
#if (0)
    ::glPushMatrix();
#endif
}
void Transform2D::glPopMatrix()
{
#if (0)
    ::glPopMatrix();
#endif
}
void Transform2D::glTranslatef(float i_x, float i_y, float i_z)
{
#if (0)
    ::glTranslatef(i_x, i_y, i_z);
#endif
}
void Transform2D::glScalef(float i_scale)
{
#if (0)
    ::glScalef(i_scale, i_scale, i_scale);
#endif
}


/*-----------------------------------EOF!-----------------------------------*/

